package com.snj.visangedu.studytime;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

import com.snj.visangedu.HomeActivity;
import com.snj.visangedu.R;
import com.snj.visangedu.drm.VideoAct;
import com.snj.visangedu.lib.CustomHttpClient;
import com.snj.visangedu.lib.Global;
import com.snj.visangedu.lib.SV_Chronometer;

public class StudyTimerFragment extends Fragment implements OnClickListener {
	private final String TAG = "StudyTimerFragment";
	
	int timepanel_topmargin = 0;
	
	int studying_time = 0;
	
	Ringtone ringtone = null;
	Vibrator vibe = null;
	
	Timepanel tp_h = null, tp_m = null, tp_s = null;
	ImageView img_h_div = null, img_m_div = null;
	ImageView img_start = null, img_setting = null, img_init = null;
	
	ImageView img_hour = null, img_minute = null, img_second = null;
	ImageView img_div_h = null, img_div_m = null;
	
	ImageView img_timerstate = null;
	
	ImageView img_edumax_temp = null;
	LinearLayout lily_edumax_menupanel = null;
	
	ImageView img_edumax = null;
		
	SV_Chronometer cm = null;
	 
	RelativeLayout rlly_setting = null;
	
	ImageView img_setting_sound = null;
	
	ImageView img_choice_sound = null;
	ImageView img_off_sound = null;
	
	RelativeLayout rlly_choicebell = null;
	ListView lstv_belllist;
	BellAdapter bell_adapter = null;
	
	ArrayList<BellItem> mListBell = new ArrayList<BellItem>();
	
	ImageView img_setting_back = null;
	
	ImageView img_choicebell_cancel = null;
	ImageView img_choicebell_save = null;
	
	RelativeLayout rlly_timepanel = null;
	
	
	ImageView img_bell_mute = null;
	ImageView img_bell_on = null;
	ImageView img_bell_off = null;
	
	long settingtime = 0;
	
	BellItem bellitem = null;
	
	View view = null;
		
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_studytimer,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		init_widget();
		
	}
	
	private void init_widget() {
		
		view = getView();
		
		cm = (SV_Chronometer)view.findViewById(R.id.chronometer2);
		cm.setOnChronometerTickListener(new OnChronometerTickListener() {
			
			@Override
			public void onChronometerTick(Chronometer chronometer) {
				// TODO Auto-generated method stub
				
				long elapsedtime = SystemClock.elapsedRealtime() - cm.getBase();
				long remainingtime = settingtime - (int)(elapsedtime / 1000);
				 
				if(remainingtime <= 0)
				{
					cm.stop();
					tp_s.SetTime(0);
					
					if(Global.pref.getBoolean("playbell", false))
					{
						ringtone =  RingtoneManager.getRingtone(getActivity(), Uri.parse(Global.pref.getString("studytimer_bell", RingtoneManager.getActualDefaultRingtoneUri(getActivity(), RingtoneManager.TYPE_RINGTONE).toString())));
						ringtone.play();
					}
					
					vibe.vibrate(new long[]{500, 100}, 0);
					
					new AsyncTask<String, String, String>() {

						@Override
						protected String doInBackground(String... params) {
							// TODO Auto-generated method stub
							
							ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
							postParameters.add(new BasicNameValuePair("os", "A"));
							postParameters.add(new BasicNameValuePair("naverid", Global.naverid));
							postParameters.add(new BasicNameValuePair("timetype", "timer"));
							postParameters.add(new BasicNameValuePair("studyingtime", String.format("%d", studying_time)));
							
			            	String result = "";
			            	JSONObject resultobject = null;
			            	
			            	try {
								result = CustomHttpClient.executeHttpPost(Global.recordstudytimeurl, postParameters);
								Log.d(TAG, result);
								
			            	}
			            	catch(Exception e)
			            	{
			            		
			            	}
							
							return null;
						}

						@Override
						protected void onPostExecute(String result) {
							// TODO Auto-generated method stub
							super.onPostExecute(result);
						}
						
					}.execute("");
					return;
				}
				
				studying_time++;
				
				int hour = (int)((remainingtime) / 3600);
				int minute = (int)(((remainingtime) / 60) % 60);
				int sec = (int)((remainingtime) % 60);
				
				
				tp_h.SetTime(hour);				
				tp_m.SetTime(minute);
				tp_s.SetTime(sec);
			}
		});
		
		img_hour = (ImageView)view.findViewById(R.id.img_studytimer_hour);
		img_minute = (ImageView)view.findViewById(R.id.img_studytimer_minute);
		img_second = (ImageView)view.findViewById(R.id.img_studytimer_second);
		
		img_div_h = (ImageView)view.findViewById(R.id.img_h_div);
		img_div_m = (ImageView)view.findViewById(R.id.img_m_div);
		
		img_timerstate = (ImageView)view.findViewById(R.id.img_studytimer_state);
		img_timerstate.setOnClickListener(this);
		
		tp_h = (Timepanel)view.findViewById(R.id.tp0);
		tp_m = (Timepanel)view.findViewById(R.id.tp1);
		tp_s = (Timepanel)view.findViewById(R.id.tp2);
		
		tp_h.SetTimeType(0);
		tp_m.SetTimeType(1);
		tp_s.SetTimeType(2);
		
		
		img_start = (ImageView)view.findViewById(R.id.img_studytimer_start);
		img_start.setOnClickListener(this);
		
		img_setting = (ImageView)view.findViewById(R.id.img_studytimer_setting);
		img_setting.setOnClickListener(this);
		
		img_init= (ImageView)view.findViewById(R.id.img_studytimer_init);
		img_init.setOnClickListener(this);
		
		rlly_setting = (RelativeLayout)view.findViewById(R.id.rlly_setting);
		
		img_setting_sound = (ImageView)view.findViewById(R.id.img_setting_bell);
		
		
		img_setting_sound.setOnClickListener(this);
		
		img_choice_sound = (ImageView)view.findViewById(R.id.img_choice_bell);
		img_choice_sound.setOnClickListener(this);
		
		img_off_sound = (ImageView)view.findViewById(R.id.img_off_bell);
		img_off_sound.setOnClickListener(this);
		
		rlly_choicebell = (RelativeLayout)view.findViewById(R.id.rlly_choice_bell);
		lstv_belllist = (ListView)view.findViewById(R.id.lstv_belllist);
		lstv_belllist.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lstv_belllist.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				//Log.d(TAG, position + mListBell.get(position).GetBellUri().toString());
				
				if(ringtone != null)
				{
					ringtone.stop();
				}
				
				ringtone = RingtoneManager.getRingtone(getActivity(), mListBell.get(position).GetBellUri());
				ringtone.play();
				
				
				bellitem = mListBell.get(position);
			}
		});
		
		img_choicebell_cancel = (ImageView)view.findViewById(R.id.img_choicebell_cancel);
		img_choicebell_save = (ImageView)view.findViewById(R.id.img_choicebell_save);
		
		img_choicebell_cancel.setOnClickListener(this);
		img_choicebell_save.setOnClickListener(this);
		
		img_edumax_temp = (ImageView)view.findViewById(R.id.img_studytimer_edumax_temp);
		img_edumax_temp.setOnClickListener(this);
		
		lily_edumax_menupanel = (LinearLayout)view.findViewById(R.id.lily_edumax_menupanel);
		
		img_edumax = (ImageView)view.findViewById(R.id.img_studytimer_edumax);
		img_edumax.setOnClickListener(this);
		
		rlly_timepanel = (RelativeLayout)view.findViewById(R.id.rlly_timepanel);
		
		img_h_div = (ImageView)view.findViewById(R.id.img_h_div);
		img_m_div = (ImageView)view.findViewById(R.id.img_m_div);
		
		int screen_width_px = getActivity().getResources().getDisplayMetrics().widthPixels;
		int screen_width_dp = Global.pxToDp(getActivity(), screen_width_px);
		
		int timepanel_width_dp = screen_width_dp - 20;
		double timepanel_width_dp100 = timepanel_width_dp / 100f;
		
		
		Log.d(TAG, "tp_width : " +  timepanel_width_dp100 * 30);
		
		RelativeLayout.LayoutParams tp_param = (RelativeLayout.LayoutParams)tp_h.getLayoutParams();
		tp_param.width = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 26));
		tp_h.setLayoutParams(tp_param);
		
		tp_param = (RelativeLayout.LayoutParams)tp_m.getLayoutParams();
		tp_param.width = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 26));
		tp_m.setLayoutParams(tp_param);
		
		tp_param = (RelativeLayout.LayoutParams)tp_s.getLayoutParams();
		tp_param.width = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 26));
		tp_s.setLayoutParams(tp_param);
		
		tp_param = (RelativeLayout.LayoutParams)img_h_div.getLayoutParams();
		tp_param.width = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 2));
		tp_param.leftMargin = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 4));
		tp_param.rightMargin = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 4));
		img_h_div.setLayoutParams(tp_param);
		
		tp_param = (RelativeLayout.LayoutParams)img_m_div.getLayoutParams();
		tp_param.width = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 2));
		tp_param.leftMargin = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 4));
		tp_param.rightMargin = Global.dpToPx(getActivity(), (int)(timepanel_width_dp100 * 4));
		img_m_div.setLayoutParams(tp_param);
		
		Log.d(TAG, "screen_width : " + screen_width_dp + ", timepanel_width_dp : " + timepanel_width_dp);
		

		img_bell_mute = (ImageView)view.findViewById(R.id.img_bell_mute);
		img_bell_on = (ImageView)view.findViewById(R.id.img_bell_on);
		img_bell_off = (ImageView)view.findViewById(R.id.img_bell_off);
		img_bell_off.setSelected(true);
		
		
		img_bell_mute.setOnClickListener(this);
		img_bell_on.setOnClickListener(this);
		img_bell_off.setOnClickListener(this);
		
		
		vibe = (Vibrator)getActivity().getSystemService(getActivity().VIBRATOR_SERVICE);
		
		
		img_setting_back = (ImageView)view.findViewById(R.id.img_setting_back);
		img_setting_back.setOnClickListener(this);
		
		LinearLayout lily_gap = new LinearLayout(getActivity());
		RelativeLayout.LayoutParams gap_param = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 1);
		gap_param.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		lily_gap.setLayoutParams(gap_param);
		lily_gap.setBackgroundResource(R.color.white_color);
		
		RelativeLayout rlly_setting_menu_panel = (RelativeLayout)view.findViewById(R.id.rlly_setting_menupanel);
		
		
		rlly_setting_menu_panel.addView(lily_gap);
		
		
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		
		SharedPreferences.Editor editor = Global.pref.edit();
		
		switch(v.getId())
		{
		case R.id.img_studytimer_start:
			
			
			if(!cm.isRunning())
			{	
				studying_time = 0;
				settingtime = (tp_h.GetTime() * 3600) + (tp_m.GetTime() * 60) + (tp_s.GetTime());
				if(settingtime <= 0)
				{
					StopTimer(true);
					return;
				}
				tp_h.SetRunningState(true);
				tp_m.SetRunningState(true);
				tp_s.SetRunningState(true);
				
				img_hour.setSelected(true);
				img_minute.setSelected(true);
				img_second.setSelected(true);
				
				img_div_h.setSelected(true);
				img_div_m.setSelected(true);
				 
				img_setting.setVisibility(View.GONE);
				img_init.setVisibility(View.VISIBLE);
				
				img_bell_off.setBackgroundResource(R.drawable.stopwatch_btn_back_disable);
				img_bell_mute.setBackgroundResource(R.drawable.stopwatch_btn_back);
				
				cm.setBase(SystemClock.elapsedRealtime());
				cm.start();
				
				img_start.setImageResource(R.drawable.stopwatch_stop);
				
				img_timerstate.setImageResource(R.drawable.studytimer_runningcircle);
				RelativeLayout.LayoutParams state_param = (RelativeLayout.LayoutParams)img_timerstate.getLayoutParams();
				state_param.topMargin = Global.dpToPx(getActivity(), 35);
				img_timerstate.setLayoutParams(state_param);
				
				RelativeLayout.LayoutParams div_param = (RelativeLayout.LayoutParams)img_h_div.getLayoutParams();
				div_param.topMargin = Global.dpToPx(getActivity(), 40);
				img_h_div.setLayoutParams(div_param);
				
				div_param = (RelativeLayout.LayoutParams)img_m_div.getLayoutParams();
				div_param.topMargin = Global.dpToPx(getActivity(), 40);
				img_m_div.setLayoutParams(div_param);
				
				
			}
			else
			{
				cm.stop();
				img_start.setImageResource(R.drawable.stopwatch_resume);
				
				img_bell_off.setBackgroundResource(R.drawable.stopwatch_btn_back);
				img_bell_mute.setBackgroundResource(R.drawable.stopwatch_btn_back_disable);
				
				new AsyncTask<String, String, String>() {

					@Override
					protected String doInBackground(String... params) {
						// TODO Auto-generated method stub
						
						ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
						postParameters.add(new BasicNameValuePair("os", "A"));
						postParameters.add(new BasicNameValuePair("naverid", Global.naverid));
						postParameters.add(new BasicNameValuePair("timetype", "timer"));
						postParameters.add(new BasicNameValuePair("studyingtime", String.format("%d", studying_time)));
						
		            	String result = "";
		            	JSONObject resultobject = null;
		            	
		            	try {
							result = CustomHttpClient.executeHttpPost(Global.recordstudytimeurl, postParameters);
							Log.d(TAG, result);
							
		            	}
		            	catch(Exception e)
		            	{
		            		
		            	}
						
						return null;
					}

					@Override
					protected void onPostExecute(String result) {
						// TODO Auto-generated method stub
						super.onPostExecute(result);
					}
					
				}.execute("");
				
			}
			break;
		case R.id.img_studytimer_state:
			if(cm.isRunning())
			{
				settingtime += 60;
			}
			else
			{
				StopTimer(true);
			}
			break;
		case R.id.img_studytimer_setting:
			rlly_setting.setVisibility(View.VISIBLE);
			break;
		case R.id.img_studytimer_init:
			StopTimer(false);
			
			
			break;
		case R.id.img_choice_bell:
			
			RingtoneManager rmgr = new RingtoneManager(getActivity());
			String currentid = "";
			int defaultid = 0;
			
			
			Uri uri = RingtoneManager.getActualDefaultRingtoneUri(getActivity(), RingtoneManager.TYPE_RINGTONE);
			
			if(uri != null)
			{
				Cursor cc = getActivity().getContentResolver().query(uri, null, null, null, null);
				cc.moveToFirst();
				//Log.d(TAG, "title : " + cc.getString(cc.getColumnIndex(MediaStore.MediaColumns._ID)));
				currentid = cc.getString(cc.getColumnIndex(MediaStore.MediaColumns._ID));
				defaultid = Integer.parseInt(currentid);
			}
			
			
			Cursor c = rmgr.getCursor();
			c.moveToFirst();			
			mListBell.clear();
			
			
			while(c.moveToNext())
			{
				Log.d(TAG, currentid.trim() + "," + c.getString(0).trim() + "|");
				if(currentid.trim().equals(c.getString(0).trim()))
				{					
					Log.d(TAG, currentid + " is true");
					mListBell.add(new BellItem(0, c.getString(1), rmgr.getRingtoneUri(c.getPosition()), true));
					
				}
				else
					mListBell.add(new BellItem(0, c.getString(1), rmgr.getRingtoneUri(c.getPosition()), false));
			}
			
			rlly_choicebell.setVisibility(View.VISIBLE);
			bell_adapter = new BellAdapter(getActivity(), R.layout.list_bell, mListBell, getActivity().getPackageName());
			
			lstv_belllist.setAdapter(bell_adapter);
			
			lstv_belllist.setItemChecked(defaultid, true);
			
			
			break;
		case R.id.img_off_bell:
			
			editor.putBoolean("playbell", false);
			editor.commit();
			Toast.makeText(getActivity(), "알림음 설정이 저장되었습니다.", Toast.LENGTH_LONG).show();
			break;
		case R.id.img_choicebell_cancel:
			break;
		case R.id.img_choicebell_save:
			Toast.makeText(getActivity(), "알림음 설정이 저장되었습니다.", Toast.LENGTH_LONG).show();
			rlly_choicebell.setVisibility(View.GONE);
			
			if(ringtone != null)
			{
				ringtone.stop();
			}
			
				
			
			if(bellitem == null)
			{				
				editor.putString("studytimer_bell", RingtoneManager.getActualDefaultRingtoneUri(getActivity(), RingtoneManager.TYPE_RINGTONE).toString());
			}
			else
			{
				editor.putString("studytimer_bell", bellitem.GetBellUri().toString());
			}
			editor.putBoolean("playbell", true);
			editor.commit();
			
			//bell_adapter.mBellList.get(lstv_belllist.getSelectedItemPosition())
			
			break;
		case R.id.img_studytimer_edumax_temp:
			if(Global.ispaid)
			{
				img_edumax_temp.setVisibility(View.GONE);
				lily_edumax_menupanel.setVisibility(View.VISIBLE);
			}
			else
			{
				
				((HomeActivity)getActivity()).open_paywindow("studytimer");
			}
			break;
		case R.id.img_studytimer_edumax:
			
			img_edumax_temp.setVisibility(View.VISIBLE);
			lily_edumax_menupanel.setVisibility(View.GONE);
			
			
			
			break;
		case R.id.img_bell_mute:
			Log.d(TAG, "mute");
			img_bell_mute.setSelected(true);
			img_bell_on.setSelected(false);
			img_bell_off.setSelected(false);
			/*
			Intent intent = new Intent(getActivity(),VideoAct.class);
			intent.putExtra("PATH","http://viewnpop.com/snj/visangedu/drm/bf.MS4"); 
			startActivity(intent);*/
			
			((HomeActivity)getActivity()).PlayMedia(Global.ex_storage + "/visangedu/bf.MS4");
			break;
		case R.id.img_bell_on:
			Log.d(TAG, "on");
			img_bell_mute.setSelected(false);
			img_bell_on.setSelected(true);
			img_bell_off.setSelected(false);
			
			/*
			Intent intent1 = new Intent(getActivity(),VideoAct.class);
			intent1.putExtra("PATH","http://studyview.co.kr/ms4/bn.MS4"); 
			startActivity(intent1);
			*/
			
			((HomeActivity)getActivity()).PlayMedia(Global.ex_storage + "/visangedu/bn.MS4");
			//Intent intent1 = new Intent(getActivity(),VideoAct.class);
			//intent1.putExtra("PATH","http://studyview.co.kr/ms4/bn.MS4"); 
			//startActivity(intent1); 
			
			
			break;
		case R.id.img_bell_off:
			Log.d(TAG, "off");

			img_bell_mute.setSelected(false);
			img_bell_on.setSelected(false);
			img_bell_off.setSelected(true);
			((HomeActivity)getActivity()).StopMedia();
			break;
		case R.id.img_setting_back:
			ClosePopup();
			break;
		}
		
		
	}
	
	void StopTimer(boolean issave)
	{
		cm.stop();
		
		
		
		tp_h.SetRunningState(false);
		tp_m.SetRunningState(false);
		tp_s.SetRunningState(false);
		
		img_hour.setSelected(false);
		img_minute.setSelected(false);
		img_second.setSelected(false);
		
		img_div_h.setSelected(false);
		img_div_m.setSelected(false);
		
		img_init.setVisibility(View.GONE);
		img_setting.setVisibility(View.VISIBLE);
		
		img_timerstate.setImageResource(R.drawable.studytimer_circle);
		
		tp_h.SetTime(0);
		tp_m.SetTime(0);
		tp_s.SetTime(0);
		
		img_start.setImageResource(R.drawable.stopwatch_start);
		
		RelativeLayout.LayoutParams state_param = (RelativeLayout.LayoutParams)img_timerstate.getLayoutParams();
		state_param.topMargin = Global.dpToPx(getActivity(), 0);
		img_timerstate.setLayoutParams(state_param);
		
		RelativeLayout.LayoutParams div_param = (RelativeLayout.LayoutParams)img_h_div.getLayoutParams();
		div_param.topMargin = Global.dpToPx(getActivity(), 0);
		img_h_div.setLayoutParams(div_param);
		
		div_param = (RelativeLayout.LayoutParams)img_m_div.getLayoutParams();
		div_param.topMargin = Global.dpToPx(getActivity(), 0);
		img_m_div.setLayoutParams(div_param);
		
		if(ringtone != null)
		{
			ringtone.stop();
		}
		
		new AsyncTask<String, String, String>() {

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("os", "A"));
				postParameters.add(new BasicNameValuePair("naverid", Global.naverid));
				postParameters.add(new BasicNameValuePair("timetype", "timer"));
				postParameters.add(new BasicNameValuePair("studyingtime", String.format("%d", studying_time)));
				
            	String result = "";
            	JSONObject resultobject = null;
            	
            	try {
					result = CustomHttpClient.executeHttpPost(Global.recordstudytimeurl, postParameters);
					Log.d(TAG, result);
					
            	}
            	catch(Exception e)
            	{
            		
            	}
				
				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
			}
			
		}.execute("");
		
		
		vibe.cancel();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		
	}
	
	public int ClosePopup()
	{
		if(ringtone != null && ringtone.isPlaying())
		{
			StopTimer(false);
			return 3;
		}
		
		vibe.cancel();
				
		if(rlly_choicebell.getVisibility() == View.VISIBLE)
		{
			rlly_choicebell.setVisibility(View.GONE);
			if(ringtone != null)
			{
				ringtone.stop();
			}
			return 1;
		}
		
		if(rlly_setting.getVisibility() == View.VISIBLE)
		{
			rlly_setting.setVisibility(View.GONE);
			if(ringtone != null)
			{
				ringtone.stop();
			}
			return 2;
		}
		
		
		
		return 0;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		if(ringtone != null)
		{
			ringtone.stop();
		}
		
		//((HomeActivity)getActivity()).StopMedia();
		
		if(cm.isRunning())
		{
			cm.stop();
			
			new AsyncTask<String, String, String>() {

				@Override
				protected String doInBackground(String... params) {
					// TODO Auto-generated method stub
					
					ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
					postParameters.add(new BasicNameValuePair("os", "A"));
					postParameters.add(new BasicNameValuePair("naverid", Global.naverid));
					postParameters.add(new BasicNameValuePair("timetype", "timer"));
					postParameters.add(new BasicNameValuePair("studyingtime", String.format("%d", studying_time)));
					
	            	String result = "";
	            	JSONObject resultobject = null;
	            	
	            	try {
						result = CustomHttpClient.executeHttpPost(Global.recordstudytimeurl, postParameters);
						Log.d(TAG, result);
						
	            	}
	            	catch(Exception e)
	            	{
	            		
	            	}
					
					return null;
				}

				@Override
				protected void onPostExecute(String result) {
					// TODO Auto-generated method stub
					super.onPostExecute(result);
				}
				
			}.execute("");
		}
		
		super.onDestroy();
		
		//CleanMemory(view);
	}
	
	
	private void CleanMemory(View v)
	{
		ViewGroup vg = (ViewGroup)v;
		
		while(vg.getChildCount() > 0)
		{
			
			if(vg.getChildAt(0) instanceof ViewGroup || vg.getChildAt(0) instanceof RelativeLayout || vg.getChildAt(0) instanceof LinearLayout)
			{
				CleanMemory(vg.getChildAt(0));
			}
			else if(vg.getChildAt(0) instanceof ImageView)
			{
				ImageView imgv = (ImageView)vg.getChildAt(0);
				
				Drawable d = imgv.getDrawable();
				if(d instanceof BitmapDrawable)
				{
					Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
					bitmap.recycle();
					bitmap = null;
				}
				
				
				d.setCallback(null);
			}
			
			vg.removeViewAt(0);
		}
		
		
	}
	
	
	
	
}

